home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14418 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.0 KB  |  105 lines

  1. Path: news.primenet.com!jstern
  2. From: jstern@primenet.com (Josh Stern)
  3. Newsgroups: comp.lang.smalltalk,comp.object,comp.lang.c++,comp.lang.java
  4. Subject: Re: The Good, the Bad, the Ugly, and the Wicked ...
  5. Date: 30 Mar 1996 01:41:02 -0700
  6. Organization: Primenet Services for the Internet
  7. Sender: root@primenet.com
  8. Message-ID: <4jis2u$hp8@nnrp1.news.primenet.com>
  9. References: <31570B8E.5A12@vmark.com> <4jbk0m$jt9@news4.digex.net> <AD7EDB45966858C12@mac-sandvik.engr.sgi.com> <3159B3E5.5281@bsis.com>
  10. X-Posted-By: jstern@usr3.primenet.com
  11.  
  12. Leonard Lehew  <leonard.lehew@bsis.com> wrote:
  13.  
  14. >>   >C++ very much has dynamic binding; lookup virtual functions.
  15. >> 
  16. >> It depends how you defined dynamic binding, could you send messages to
  17. >> arbitrary methods in C++ during runtime (or java, by the way)?
  18.  
  19. >C++ implements dynamic binding through virtual methods. When a virtual 
  20. >member function is invoked (i.e., when a message is sent to an object)
  21. >the actual run time object must be of the class the compiler "expects" or
  22. >it must be of a class derived from the "expected" class. This means
  23. >that the polymorphism only extends to classes which are part of the
  24. >same inheritance tree.
  25. >
  26. >Smalltalk by comparison does not impose this restriction. 
  27.  
  28. Actually C++ doesn't either, see below.
  29.  
  30. >You can send
  31. >any message selector to any object at runtime. If the receiving object
  32. >has a corresponding method, it will be invoked, regardless of its
  33. >inheritance.
  34. >
  35. >Which is better depends on your perspective and needs. The strong type
  36. >checking in C++ detects certain errors before testing, and C++ apps
  37. >generally perform better. On the other hand, there are many circumstances
  38. >where I want polymorphic behavior, and inheritance is just not the most
  39. >natural, convenient, or elegant way to achieve it.
  40.  
  41. It should be pointed out that templates provide C++ with
  42. a different, *non-dynamic method of polymorphism*.  The fact
  43. that it is non-dynamic means that it is not polymorphic
  44. from the point of view of the back end of the compiler -
  45. and strict type checking is enforced - but templates are 
  46. *polymorphic from the point of view of the programmer*.
  47. Templates allow programming to be done generically.
  48. I can write a function like this:
  49.  
  50. template <class T1, class T2>
  51. T1 funct(T1 x, T2 y) { return x.methoda + y.methodb; };
  52.  
  53. Then elsewhere in the code one can call funct
  54. like this:
  55. String s1,s2;
  56. String s3 = funct(s1,s2);
  57.  
  58. or like this:
  59.  
  60. int x1; float x2, x3;
  61. x3 = funct(x1, x1);
  62.  
  63. In each case, a different version of funct
  64. is automatically inferred and created by the
  65. compiler.  The compiler doesn't mind doing this
  66. so long as there is a 'methoda' for the type of 
  67. its first argument, a 'methodb' for the type of 
  68. its second argument, and an operator+() (which 
  69. can be either a method of the first argument or 
  70. a global function in the current context) that 
  71. takes an ordered pair of the appropriate type 
  72. and returns an argument of the first type.
  73.  
  74. The syntax is a bit ugly, but this technique combines 
  75. the main advantages of dynamic polymorphism and
  76. static type checking.  It is essentially static
  77. polymorphism, and it is polymorphism and not 
  78. dynamic binding per se that
  79. is really what gives the programmer flexibility
  80. and leads to code reuse.
  81.  
  82. Classes can also be templates, with parameterized
  83. types used to define any of their data, their
  84. methods, and/or their hierarchy.
  85.  
  86. The only two unfortunate aspects of these techniques
  87. are a) that the return type of functions and methods
  88. cannot be used for inference about their
  89. parameterized types, though it may be parameterized
  90. as in the example above, and b) different executable
  91. code is generated for each different parameterization
  92. that is actually invoked, leading to great speed
  93. for small programs, but potentially large executables
  94. for large programs that are not careful about
  95. what all they are instantiating.
  96.  
  97. - Josh
  98.  
  99.  
  100. --
  101. -------------------------------------------------------------------------------
  102. jstern
  103. jstern@primenet.com
  104. -------------------------------------------------------------------------------
  105.